import permissionStore from "@/store/permission";
import { IRouter } from "./type";
//页面内操作权限，如按钮
let btnPermission: string[] = [];
//处理路由数据
export const dealRouteData = (permissionBox: Array<IRouter>) => {
    btnPermission = [];
    let permission = permissionStore();
    let result = dealPermissions(permissionBox);
    //路由层级
    let menuRouter = filterAsyncRouter(result);
    //扁平化路由
    let accessedRouters = flatAsyncRoutes(menuRouter);
    // permission更新
    permission.updateRoutes(menuRouter);
    permission.updateBtnPermissions(btnPermission);
    return {
        btnPermission,
        accessedRouters,
        menuRouter,
    };
};
//处理用户菜单中权限信息
export function dealPermissions(permissionBox: Array<IRouter>): Array<IRouter> {
    if (!permissionBox || !permissionBox.length) return [];
    permissionBox.map((item: IRouter) => {
        item.name = item.name;
        item.meta = { title: item.name, icon: item.icon };
        if (item.type == "M") {
            let res = getFirstMenu(item);
            item.redirect = res.path || "/";
        }
        if (item.children && item.children.length) {
            item.children = dealPermissions(item.children);
        } else {
            item.children = null;
        }
        if (item.type == "F") {
            btnPermission.push(item.path);
        }
        if (item.type == "C") {
            if (!hasChildren(item)) {
                item.children = null;
            }
            item.component = loadComponent(item.path);
        }
    });
    return permissionBox;
}

//是否有显示的子页面内容
export const hasChildren = (item: IRouter) => {
    if (item.children) {
        let isHas = !item.children.every(item => item.extend == 0) || item.children.some(item => item.type == 'F')
        if (isHas) {
            return false;
        } else {
            return true
        }
    } else {
        return false;
    }
};
//权限菜单中第一个实质性的菜单
export const getFirstMenu = (item: any): any => {
    if (!item) return {};
    if (item.type == "C") {
        return item;
    }
    if (item.type == "M") {
        // let getItem = item && item.children.find((menu: any) => menu.type == "C");
        let getItem = item && item.children;
        if (getItem) {
            return getFirstMenu(item.children[0]);
        } else {
            return item;
        }
    }
};

//转换
export const filterAsyncRouter = (
    routerMap: Array<IRouter>
): Array<IRouter> => {
    const accessedRouters: Array<IRouter> = [];
    routerMap.forEach((item: IRouter) => {
        item.meta = item.meta ? item.meta : {};
        //MAP转路由对象
        var route: IRouter = {
            path: item.path,
            label: item.name,
            meta: item.meta,
            icon: item.icon,
            extend:item.extend,
            redirect: item.redirect,
            children: item.children ? filterAsyncRouter(item.children) : [],
            component: loadComponent(item.path),
        };
        accessedRouters.push(route);
    });
    return accessedRouters;
};
//动态加载路由
export const loadComponent = (component: string) => {
    // if (component && type != 'F' && type != "M")
    if (component) {
        let url = `../views${component}.vue`;
        return () => import(/* @vite-ignore */ url);
    }
};
//路由扁平化
export const flatAsyncRoutes = (
    routes: Array<IRouter>,
    breadcrumb: Array<IRouter> = []
) => {
    let res: Array<IRouter> = [];
    routes.forEach((route) => {
        const tmp = { ...route };
        if (tmp.children) {
            let childrenBreadcrumb = [...breadcrumb];
            childrenBreadcrumb.push(route);
            let tmpRoute = { ...route };
            tmpRoute.meta && (tmpRoute.meta.breadcrumb = childrenBreadcrumb);
            delete tmpRoute.children;
            res.push(tmpRoute);
            let childrenRoutes = flatAsyncRoutes(tmp.children, childrenBreadcrumb);
            childrenRoutes.map((item) => {
                res.push(item);
            });
        } else {
            let tmpBreadcrumb = [...breadcrumb];
            tmpBreadcrumb.push(tmp);
            tmp.meta && (tmp.meta.breadcrumb = tmpBreadcrumb);
            res.push(tmp);
        }
    });
    return res;
};
